home *** CD-ROM | disk | FTP | other *** search
/ Freelog 115 / FreelogNo115-MaiJuin2013.iso / Internet / Filezilla Server / FileZilla_Server-0_9_41.exe / source / tinyxml / tinyxml.cpp < prev    next >
C/C++ Source or Header  |  2011-11-06  |  39KB  |  1,864 lines

  1. /*
  2. www.sourceforge.net/projects/tinyxml
  3. Original code by Lee Thomason (www.grinninglizard.com)
  4.  
  5. This software is provided 'as-is', without any express or implied
  6. warranty. In no event will the authors be held liable for any
  7. damages arising from the use of this software.
  8.  
  9. Permission is granted to anyone to use this software for any
  10. purpose, including commercial applications, and to alter it and
  11. redistribute it freely, subject to the following restrictions:
  12.  
  13. 1. The origin of this software must not be misrepresented; you must
  14. not claim that you wrote the original software. If you use this
  15. software in a product, an acknowledgment in the product documentation
  16. would be appreciated but is not required.
  17.  
  18. 2. Altered source versions must be plainly marked as such, and
  19. must not be misrepresented as being the original software.
  20.  
  21. 3. This notice may not be removed or altered from any source
  22. distribution.
  23. */
  24.  
  25. #include <ctype.h>
  26.  
  27. #ifdef TIXML_USE_STL
  28. #include <sstream>
  29. #include <iostream>
  30. #endif
  31.  
  32. #include "tinyxml.h"
  33.  
  34. FILE* TiXmlFOpen( const char* filename, const char* mode );
  35.  
  36. bool TiXmlBase::condenseWhiteSpace = true;
  37.  
  38. // Microsoft compiler security
  39. FILE* TiXmlFOpen( const char* filename, const char* mode )
  40. {
  41.     #if defined(_MSC_VER) && (_MSC_VER >= 1400 )
  42.         FILE* fp = 0;
  43.         errno_t err = fopen_s( &fp, filename, mode );
  44.         if ( !err && fp )
  45.             return fp;
  46.         return 0;
  47.     #else
  48.         return fopen( filename, mode );
  49.     #endif
  50. }
  51.  
  52. void TiXmlBase::EncodeString( const TIXML_STRING& str, TIXML_STRING* outString )
  53. {
  54.     int i=0;
  55.  
  56.     while( i<(int)str.length() )
  57.     {
  58.         unsigned char c = (unsigned char) str[i];
  59.  
  60.         if ( c == '&' )
  61.         {
  62.             outString->append( entity[0].str, entity[0].strLength );
  63.             ++i;
  64.         }
  65.         else if ( c == '<' )
  66.         {
  67.             outString->append( entity[1].str, entity[1].strLength );
  68.             ++i;
  69.         }
  70.         else if ( c == '>' )
  71.         {
  72.             outString->append( entity[2].str, entity[2].strLength );
  73.             ++i;
  74.         }
  75.         else if ( c == '\"' )
  76.         {
  77.             outString->append( entity[3].str, entity[3].strLength );
  78.             ++i;
  79.         }
  80.         else if ( c == '\'' )
  81.         {
  82.             outString->append( entity[4].str, entity[4].strLength );
  83.             ++i;
  84.         }
  85.         else if ( c < 32 )
  86.         {
  87.             // Easy pass at non-alpha/numeric/symbol
  88.             // Below 32 is symbolic.
  89.             char buf[ 32 ];
  90.             
  91.             #if defined(TIXML_SNPRINTF)        
  92.                 TIXML_SNPRINTF( buf, sizeof(buf), "&#x%02X;", (unsigned) ( c & 0xff ) );
  93.             #else
  94.                 sprintf( buf, "&#x%02X;", (unsigned) ( c & 0xff ) );
  95.             #endif        
  96.  
  97.             //*ME:    warning C4267: convert 'size_t' to 'int'
  98.             //*ME:    Int-Cast to make compiler happy ...
  99.             outString->append( buf, (int)strlen( buf ) );
  100.             ++i;
  101.         }
  102.         else
  103.         {
  104.             //char realc = (char) c;
  105.             //outString->append( &realc, 1 );
  106.             *outString += (char) c;    // somewhat more efficient function call.
  107.             ++i;
  108.         }
  109.     }
  110. }
  111.  
  112.  
  113. TiXmlNode::TiXmlNode( NodeType _type ) : TiXmlBase()
  114. {
  115.     parent = 0;
  116.     type = _type;
  117.     firstChild = 0;
  118.     lastChild = 0;
  119.     prev = 0;
  120.     next = 0;
  121. }
  122.  
  123.  
  124. TiXmlNode::~TiXmlNode()
  125. {
  126.     TiXmlNode* node = firstChild;
  127.     TiXmlNode* temp = 0;
  128.  
  129.     while ( node )
  130.     {
  131.         temp = node;
  132.         node = node->next;
  133.         delete temp;
  134.     }    
  135. }
  136.  
  137.  
  138. void TiXmlNode::CopyTo( TiXmlNode* target ) const
  139. {
  140.     target->SetValue (value.c_str() );
  141.     target->userData = userData; 
  142.     target->location = location;
  143. }
  144.  
  145.  
  146. void TiXmlNode::Clear()
  147. {
  148.     TiXmlNode* node = firstChild;
  149.     TiXmlNode* temp = 0;
  150.  
  151.     while ( node )
  152.     {
  153.         temp = node;
  154.         node = node->next;
  155.         delete temp;
  156.     }    
  157.  
  158.     firstChild = 0;
  159.     lastChild = 0;
  160. }
  161.  
  162.  
  163. TiXmlNode* TiXmlNode::LinkEndChild( TiXmlNode* node )
  164. {
  165.     assert( node->parent == 0 || node->parent == this );
  166.     assert( node->GetDocument() == 0 || node->GetDocument() == this->GetDocument() );
  167.  
  168.     if ( node->Type() == TiXmlNode::TINYXML_DOCUMENT )
  169.     {
  170.         delete node;
  171.         if ( GetDocument() ) 
  172.             GetDocument()->SetError( TIXML_ERROR_DOCUMENT_TOP_ONLY, 0, 0, TIXML_ENCODING_UNKNOWN );
  173.         return 0;
  174.     }
  175.  
  176.     node->parent = this;
  177.  
  178.     node->prev = lastChild;
  179.     node->next = 0;
  180.  
  181.     if ( lastChild )
  182.         lastChild->next = node;
  183.     else
  184.         firstChild = node;            // it was an empty list.
  185.  
  186.     lastChild = node;
  187.     return node;
  188. }
  189.  
  190.  
  191. TiXmlNode* TiXmlNode::InsertEndChild( const TiXmlNode& addThis )
  192. {
  193.     if ( addThis.Type() == TiXmlNode::TINYXML_DOCUMENT )
  194.     {
  195.         if ( GetDocument() ) 
  196.             GetDocument()->SetError( TIXML_ERROR_DOCUMENT_TOP_ONLY, 0, 0, TIXML_ENCODING_UNKNOWN );
  197.         return 0;
  198.     }
  199.     TiXmlNode* node = addThis.Clone();
  200.     if ( !node )
  201.         return 0;
  202.  
  203.     return LinkEndChild( node );
  204. }
  205.  
  206.  
  207. TiXmlNode* TiXmlNode::InsertBeforeChild( TiXmlNode* beforeThis, const TiXmlNode& addThis )
  208. {    
  209.     if ( !beforeThis || beforeThis->parent != this ) {
  210.         return 0;
  211.     }
  212.     if ( addThis.Type() == TiXmlNode::TINYXML_DOCUMENT )
  213.     {
  214.         if ( GetDocument() ) 
  215.             GetDocument()->SetError( TIXML_ERROR_DOCUMENT_TOP_ONLY, 0, 0, TIXML_ENCODING_UNKNOWN );
  216.         return 0;
  217.     }
  218.  
  219.     TiXmlNode* node = addThis.Clone();
  220.     if ( !node )
  221.         return 0;
  222.     node->parent = this;
  223.  
  224.     node->next = beforeThis;
  225.     node->prev = beforeThis->prev;
  226.     if ( beforeThis->prev )
  227.     {
  228.         beforeThis->prev->next = node;
  229.     }
  230.     else
  231.     {
  232.         assert( firstChild == beforeThis );
  233.         firstChild = node;
  234.     }
  235.     beforeThis->prev = node;
  236.     return node;
  237. }
  238.  
  239.  
  240. TiXmlNode* TiXmlNode::InsertAfterChild( TiXmlNode* afterThis, const TiXmlNode& addThis )
  241. {
  242.     if ( !afterThis || afterThis->parent != this ) {
  243.         return 0;
  244.     }
  245.     if ( addThis.Type() == TiXmlNode::TINYXML_DOCUMENT )
  246.     {
  247.         if ( GetDocument() ) 
  248.             GetDocument()->SetError( TIXML_ERROR_DOCUMENT_TOP_ONLY, 0, 0, TIXML_ENCODING_UNKNOWN );
  249.         return 0;
  250.     }
  251.  
  252.     TiXmlNode* node = addThis.Clone();
  253.     if ( !node )
  254.         return 0;
  255.     node->parent = this;
  256.  
  257.     node->prev = afterThis;
  258.     node->next = afterThis->next;
  259.     if ( afterThis->next )
  260.     {
  261.         afterThis->next->prev = node;
  262.     }
  263.     else
  264.     {
  265.         assert( lastChild == afterThis );
  266.         lastChild = node;
  267.     }
  268.     afterThis->next = node;
  269.     return node;
  270. }
  271.  
  272.  
  273. TiXmlNode* TiXmlNode::ReplaceChild( TiXmlNode* replaceThis, const TiXmlNode& withThis )
  274. {
  275.     if ( !replaceThis )
  276.         return 0;
  277.  
  278.     if ( replaceThis->parent != this )
  279.         return 0;
  280.  
  281.     if ( withThis.ToDocument() ) {
  282.         // A document can never be a child.    Thanks to Noam.
  283.         TiXmlDocument* document = GetDocument();
  284.         if ( document ) 
  285.             document->SetError( TIXML_ERROR_DOCUMENT_TOP_ONLY, 0, 0, TIXML_ENCODING_UNKNOWN );
  286.         return 0;
  287.     }
  288.  
  289.     TiXmlNode* node = withThis.Clone();
  290.     if ( !node )
  291.         return 0;
  292.  
  293.     node->next = replaceThis->next;
  294.     node->prev = replaceThis->prev;
  295.  
  296.     if ( replaceThis->next )
  297.         replaceThis->next->prev = node;
  298.     else
  299.         lastChild = node;
  300.  
  301.     if ( replaceThis->prev )
  302.         replaceThis->prev->next = node;
  303.     else
  304.         firstChild = node;
  305.  
  306.     delete replaceThis;
  307.     node->parent = this;
  308.     return node;
  309. }
  310.  
  311.  
  312. bool TiXmlNode::RemoveChild( TiXmlNode* removeThis )
  313. {
  314.     if ( !removeThis ) {
  315.         return false;
  316.     }
  317.  
  318.     if ( removeThis->parent != this )
  319.     {    
  320.         assert( 0 );
  321.         return false;
  322.     }
  323.  
  324.     if ( removeThis->next )
  325.         removeThis->next->prev = removeThis->prev;
  326.     else
  327.         lastChild = removeThis->prev;
  328.  
  329.     if ( removeThis->prev )
  330.         removeThis->prev->next = removeThis->next;
  331.     else
  332.         firstChild = removeThis->next;
  333.  
  334.     delete removeThis;
  335.     return true;
  336. }
  337.  
  338. const TiXmlNode* TiXmlNode::FirstChild( const char * _value ) const
  339. {
  340.     const TiXmlNode* node;
  341.     for ( node = firstChild; node; node = node->next )
  342.     {
  343.         if ( strcmp( node->Value(), _value ) == 0 )
  344.             return node;
  345.     }
  346.     return 0;
  347. }
  348.  
  349.  
  350. const TiXmlNode* TiXmlNode::LastChild( const char * _value ) const
  351. {
  352.     const TiXmlNode* node;
  353.     for ( node = lastChild; node; node = node->prev )
  354.     {
  355.         if ( strcmp( node->Value(), _value ) == 0 )
  356.             return node;
  357.     }
  358.     return 0;
  359. }
  360.  
  361.  
  362. const TiXmlNode* TiXmlNode::IterateChildren( const TiXmlNode* previous ) const
  363. {
  364.     if ( !previous )
  365.     {
  366.         return FirstChild();
  367.     }
  368.     else
  369.     {
  370.         assert( previous->parent == this );
  371.         return previous->NextSibling();
  372.     }
  373. }
  374.  
  375.  
  376. const TiXmlNode* TiXmlNode::IterateChildren( const char * val, const TiXmlNode* previous ) const
  377. {
  378.     if ( !previous )
  379.     {
  380.         return FirstChild( val );
  381.     }
  382.     else
  383.     {
  384.         assert( previous->parent == this );
  385.         return previous->NextSibling( val );
  386.     }
  387. }
  388.  
  389.  
  390. const TiXmlNode* TiXmlNode::NextSibling( const char * _value ) const 
  391. {
  392.     const TiXmlNode* node;
  393.     for ( node = next; node; node = node->next )
  394.     {
  395.         if ( strcmp( node->Value(), _value ) == 0 )
  396.             return node;
  397.     }
  398.     return 0;
  399. }
  400.  
  401.  
  402. const TiXmlNode* TiXmlNode::PreviousSibling( const char * _value ) const
  403. {
  404.     const TiXmlNode* node;
  405.     for ( node = prev; node; node = node->prev )
  406.     {
  407.         if ( strcmp( node->Value(), _value ) == 0 )
  408.             return node;
  409.     }
  410.     return 0;
  411. }
  412.  
  413.  
  414. void TiXmlElement::RemoveAttribute( const char * name )
  415. {
  416.     #ifdef TIXML_USE_STL
  417.     TIXML_STRING str( name );
  418.     TiXmlAttribute* node = attributeSet.Find( str );
  419.     #else
  420.     TiXmlAttribute* node = attributeSet.Find( name );
  421.     #endif
  422.     if ( node )
  423.     {
  424.         attributeSet.Remove( node );
  425.         delete node;
  426.     }
  427. }
  428.  
  429. const TiXmlElement* TiXmlNode::FirstChildElement() const
  430. {
  431.     const TiXmlNode* node;
  432.  
  433.     for (    node = FirstChild();
  434.             node;
  435.             node = node->NextSibling() )
  436.     {
  437.         if ( node->ToElement() )
  438.             return node->ToElement();
  439.     }
  440.     return 0;
  441. }
  442.  
  443.  
  444. const TiXmlElement* TiXmlNode::FirstChildElement( const char * _value ) const
  445. {
  446.     const TiXmlNode* node;
  447.  
  448.     for (    node = FirstChild( _value );
  449.             node;
  450.             node = node->NextSibling( _value ) )
  451.     {
  452.         if ( node->ToElement() )
  453.             return node->ToElement();
  454.     }
  455.     return 0;
  456. }
  457.  
  458.  
  459. const TiXmlElement* TiXmlNode::NextSiblingElement() const
  460. {
  461.     const TiXmlNode* node;
  462.  
  463.     for (    node = NextSibling();
  464.             node;
  465.             node = node->NextSibling() )
  466.     {
  467.         if ( node->ToElement() )
  468.             return node->ToElement();
  469.     }
  470.     return 0;
  471. }
  472.  
  473.  
  474. const TiXmlElement* TiXmlNode::NextSiblingElement( const char * _value ) const
  475. {
  476.     const TiXmlNode* node;
  477.  
  478.     for (    node = NextSibling( _value );
  479.             node;
  480.             node = node->NextSibling( _value ) )
  481.     {
  482.         if ( node->ToElement() )
  483.             return node->ToElement();
  484.     }
  485.     return 0;
  486. }
  487.  
  488.  
  489. const TiXmlDocument* TiXmlNode::GetDocument() const
  490. {
  491.     const TiXmlNode* node;
  492.  
  493.     for( node = this; node; node = node->parent )
  494.     {
  495.         if ( node->ToDocument() )
  496.             return node->ToDocument();
  497.     }
  498.     return 0;
  499. }
  500.  
  501.  
  502. TiXmlElement::TiXmlElement (const char * _value)
  503.     : TiXmlNode( TiXmlNode::TINYXML_ELEMENT )
  504. {
  505.     firstChild = lastChild = 0;
  506.     value = _value;
  507. }
  508.  
  509.  
  510. #ifdef TIXML_USE_STL
  511. TiXmlElement::TiXmlElement( const std::string& _value ) 
  512.     : TiXmlNode( TiXmlNode::TINYXML_ELEMENT )
  513. {
  514.     firstChild = lastChild = 0;
  515.     value = _value;
  516. }
  517. #endif
  518.  
  519.  
  520. TiXmlElement::TiXmlElement( const TiXmlElement& copy)
  521.     : TiXmlNode( TiXmlNode::TINYXML_ELEMENT )
  522. {
  523.     firstChild = lastChild = 0;
  524.     copy.CopyTo( this );    
  525. }
  526.  
  527.  
  528. TiXmlElement& TiXmlElement::operator=( const TiXmlElement& base )
  529. {
  530.     ClearThis();
  531.     base.CopyTo( this );
  532.     return *this;
  533. }
  534.  
  535.  
  536. TiXmlElement::~TiXmlElement()
  537. {
  538.     ClearThis();
  539. }
  540.  
  541.  
  542. void TiXmlElement::ClearThis()
  543. {
  544.     Clear();
  545.     while( attributeSet.First() )
  546.     {
  547.         TiXmlAttribute* node = attributeSet.First();
  548.         attributeSet.Remove( node );
  549.         delete node;
  550.     }
  551. }
  552.  
  553.  
  554. const char* TiXmlElement::Attribute( const char* name ) const
  555. {
  556.     const TiXmlAttribute* node = attributeSet.Find( name );
  557.     if ( node )
  558.         return node->Value();
  559.     return 0;
  560. }
  561.  
  562.  
  563. #ifdef TIXML_USE_STL
  564. const std::string* TiXmlElement::Attribute( const std::string& name ) const
  565. {
  566.     const TiXmlAttribute* attrib = attributeSet.Find( name );
  567.     if ( attrib )
  568.         return &attrib->ValueStr();
  569.     return 0;
  570. }
  571. #endif
  572.  
  573.  
  574. const char* TiXmlElement::Attribute( const char* name, int* i ) const
  575. {
  576.     const TiXmlAttribute* attrib = attributeSet.Find( name );
  577.     const char* result = 0;
  578.  
  579.     if ( attrib ) {
  580.         result = attrib->Value();
  581.         if ( i ) {
  582.             attrib->QueryIntValue( i );
  583.         }
  584.     }
  585.     return result;
  586. }
  587.  
  588.  
  589. #ifdef TIXML_USE_STL
  590. const std::string* TiXmlElement::Attribute( const std::string& name, int* i ) const
  591. {
  592.     const TiXmlAttribute* attrib = attributeSet.Find( name );
  593.     const std::string* result = 0;
  594.  
  595.     if ( attrib ) {
  596.         result = &attrib->ValueStr();
  597.         if ( i ) {
  598.             attrib->QueryIntValue( i );
  599.         }
  600.     }
  601.     return result;
  602. }
  603. #endif
  604.  
  605.  
  606. const char* TiXmlElement::Attribute( const char* name, double* d ) const
  607. {
  608.     const TiXmlAttribute* attrib = attributeSet.Find( name );
  609.     const char* result = 0;
  610.  
  611.     if ( attrib ) {
  612.         result = attrib->Value();
  613.         if ( d ) {
  614.             attrib->QueryDoubleValue( d );
  615.         }
  616.     }
  617.     return result;
  618. }
  619.  
  620.  
  621. #ifdef TIXML_USE_STL
  622. const std::string* TiXmlElement::Attribute( const std::string& name, double* d ) const
  623. {
  624.     const TiXmlAttribute* attrib = attributeSet.Find( name );
  625.     const std::string* result = 0;
  626.  
  627.     if ( attrib ) {
  628.         result = &attrib->ValueStr();
  629.         if ( d ) {
  630.             attrib->QueryDoubleValue( d );
  631.         }
  632.     }
  633.     return result;
  634. }
  635. #endif
  636.  
  637.  
  638. int TiXmlElement::QueryIntAttribute( const char* name, int* ival ) const
  639. {
  640.     const TiXmlAttribute* attrib = attributeSet.Find( name );
  641.     if ( !attrib )
  642.         return TIXML_NO_ATTRIBUTE;
  643.     return attrib->QueryIntValue( ival );
  644. }
  645.  
  646.  
  647. int TiXmlElement::QueryUnsignedAttribute( const char* name, unsigned* value ) const
  648. {
  649.     const TiXmlAttribute* node = attributeSet.Find( name );
  650.     if ( !node )
  651.         return TIXML_NO_ATTRIBUTE;
  652.  
  653.     int ival = 0;
  654.     int result = node->QueryIntValue( &ival );
  655.     *value = (unsigned)ival;
  656.     return result;
  657. }
  658.  
  659.  
  660. int TiXmlElement::QueryBoolAttribute( const char* name, bool* bval ) const
  661. {
  662.     const TiXmlAttribute* node = attributeSet.Find( name );
  663.     if ( !node )
  664.         return TIXML_NO_ATTRIBUTE;
  665.     
  666.     int result = TIXML_WRONG_TYPE;
  667.     if (    StringEqual( node->Value(), "true", true, TIXML_ENCODING_UNKNOWN ) 
  668.          || StringEqual( node->Value(), "yes", true, TIXML_ENCODING_UNKNOWN ) 
  669.          || StringEqual( node->Value(), "1", true, TIXML_ENCODING_UNKNOWN ) ) 
  670.     {
  671.         *bval = true;
  672.         result = TIXML_SUCCESS;
  673.     }
  674.     else if (    StringEqual( node->Value(), "false", true, TIXML_ENCODING_UNKNOWN ) 
  675.               || StringEqual( node->Value(), "no", true, TIXML_ENCODING_UNKNOWN ) 
  676.               || StringEqual( node->Value(), "0", true, TIXML_ENCODING_UNKNOWN ) ) 
  677.     {
  678.         *bval = false;
  679.         result = TIXML_SUCCESS;
  680.     }
  681.     return result;
  682. }
  683.  
  684.  
  685.  
  686. #ifdef TIXML_USE_STL
  687. int TiXmlElement::QueryIntAttribute( const std::string& name, int* ival ) const
  688. {
  689.     const TiXmlAttribute* attrib = attributeSet.Find( name );
  690.     if ( !attrib )
  691.         return TIXML_NO_ATTRIBUTE;
  692.     return attrib->QueryIntValue( ival );
  693. }
  694. #endif
  695.  
  696.  
  697. int TiXmlElement::QueryDoubleAttribute( const char* name, double* dval ) const
  698. {
  699.     const TiXmlAttribute* attrib = attributeSet.Find( name );
  700.     if ( !attrib )
  701.         return TIXML_NO_ATTRIBUTE;
  702.     return attrib->QueryDoubleValue( dval );
  703. }
  704.  
  705.  
  706. #ifdef TIXML_USE_STL
  707. int TiXmlElement::QueryDoubleAttribute( const std::string& name, double* dval ) const
  708. {
  709.     const TiXmlAttribute* attrib = attributeSet.Find( name );
  710.     if ( !attrib )
  711.         return TIXML_NO_ATTRIBUTE;
  712.     return attrib->QueryDoubleValue( dval );
  713. }
  714. #endif
  715.  
  716.  
  717. void TiXmlElement::SetAttribute( const char * name, int val )
  718. {    
  719.     TiXmlAttribute* attrib = attributeSet.FindOrCreate( name );
  720.     if ( attrib ) {
  721.         attrib->SetIntValue( val );
  722.     }
  723. }
  724.  
  725.  
  726. #ifdef TIXML_USE_STL
  727. void TiXmlElement::SetAttribute( const std::string& name, int val )
  728. {    
  729.     TiXmlAttribute* attrib = attributeSet.FindOrCreate( name );
  730.     if ( attrib ) {
  731.         attrib->SetIntValue( val );
  732.     }
  733. }
  734. #endif
  735.  
  736.  
  737. void TiXmlElement::SetDoubleAttribute( const char * name, double val )
  738. {    
  739.     TiXmlAttribute* attrib = attributeSet.FindOrCreate( name );
  740.     if ( attrib ) {
  741.         attrib->SetDoubleValue( val );
  742.     }
  743. }
  744.  
  745.  
  746. #ifdef TIXML_USE_STL
  747. void TiXmlElement::SetDoubleAttribute( const std::string& name, double val )
  748. {    
  749.     TiXmlAttribute* attrib = attributeSet.FindOrCreate( name );
  750.     if ( attrib ) {
  751.         attrib->SetDoubleValue( val );
  752.     }
  753. }
  754. #endif 
  755.  
  756.  
  757. void TiXmlElement::SetAttribute( const char * cname, const char * cvalue )
  758. {
  759.     TiXmlAttribute* attrib = attributeSet.FindOrCreate( cname );
  760.     if ( attrib ) {
  761.         attrib->SetValue( cvalue );
  762.     }
  763. }
  764.  
  765.  
  766. #ifdef TIXML_USE_STL
  767. void TiXmlElement::SetAttribute( const std::string& _name, const std::string& _value )
  768. {
  769.     TiXmlAttribute* attrib = attributeSet.FindOrCreate( _name );
  770.     if ( attrib ) {
  771.         attrib->SetValue( _value );
  772.     }
  773. }
  774. #endif
  775.  
  776.  
  777. void TiXmlElement::Print( FILE* cfile, int depth ) const
  778. {
  779.     int i;
  780.     assert( cfile );
  781.     for ( i=0; i<depth; i++ ) {
  782.         fprintf( cfile, "    " );
  783.     }
  784.  
  785.     fprintf( cfile, "<%s", value.c_str() );
  786.  
  787.     const TiXmlAttribute* attrib;
  788.     for ( attrib = attributeSet.First(); attrib; attrib = attrib->Next() )
  789.     {
  790.         fprintf( cfile, " " );
  791.         attrib->Print( cfile, depth );
  792.     }
  793.  
  794.     // There are 3 different formatting approaches:
  795.     // 1) An element without children is printed as a <foo /> node
  796.     // 2) An element with only a text child is printed as <foo> text </foo>
  797.     // 3) An element with children is printed on multiple lines.
  798.     TiXmlNode* node;
  799.     if ( !firstChild )
  800.     {
  801.         fprintf( cfile, " />" );
  802.     }
  803.     else if ( firstChild == lastChild && firstChild->ToText() )
  804.     {
  805.         fprintf( cfile, ">" );
  806.         firstChild->Print( cfile, depth + 1 );
  807.         fprintf( cfile, "</%s>", value.c_str() );
  808.     }
  809.     else
  810.     {
  811.         fprintf( cfile, ">" );
  812.  
  813.         for ( node = firstChild; node; node=node->NextSibling() )
  814.         {
  815.             if ( !node->ToText() )
  816.             {
  817.                 fprintf( cfile, "\n" );
  818.             }
  819.             node->Print( cfile, depth+1 );
  820.         }
  821.         fprintf( cfile, "\n" );
  822.         for( i=0; i<depth; ++i ) {
  823.             fprintf( cfile, "    " );
  824.         }
  825.         fprintf( cfile, "</%s>", value.c_str() );
  826.     }
  827. }
  828.  
  829.  
  830. void TiXmlElement::CopyTo( TiXmlElement* target ) const
  831. {
  832.     // superclass:
  833.     TiXmlNode::CopyTo( target );
  834.  
  835.     // Element class: 
  836.     // Clone the attributes, then clone the children.
  837.     const TiXmlAttribute* attribute = 0;
  838.     for(    attribute = attributeSet.First();
  839.     attribute;
  840.     attribute = attribute->Next() )
  841.     {
  842.         target->SetAttribute( attribute->Name(), attribute->Value() );
  843.     }
  844.  
  845.     TiXmlNode* node = 0;
  846.     for ( node = firstChild; node; node = node->NextSibling() )
  847.     {
  848.         target->LinkEndChild( node->Clone() );
  849.     }
  850. }
  851.  
  852. bool TiXmlElement::Accept( TiXmlVisitor* visitor ) const
  853. {
  854.     if ( visitor->VisitEnter( *this, attributeSet.First() ) ) 
  855.     {
  856.         for ( const TiXmlNode* node=FirstChild(); node; node=node->NextSibling() )
  857.         {
  858.             if ( !node->Accept( visitor ) )
  859.                 break;
  860.         }
  861.     }
  862.     return visitor->VisitExit( *this );
  863. }
  864.  
  865.  
  866. TiXmlNode* TiXmlElement::Clone() const
  867. {
  868.     TiXmlElement* clone = new TiXmlElement( Value() );
  869.     if ( !clone )
  870.         return 0;
  871.  
  872.     CopyTo( clone );
  873.     return clone;
  874. }
  875.  
  876.  
  877. const char* TiXmlElement::GetText() const
  878. {
  879.     const TiXmlNode* child = this->FirstChild();
  880.     if ( child ) {
  881.         const TiXmlText* childText = child->ToText();
  882.         if ( childText ) {
  883.             return childText->Value();
  884.         }
  885.     }
  886.     return 0;
  887. }
  888.  
  889.  
  890. TiXmlDocument::TiXmlDocument() : TiXmlNode( TiXmlNode::TINYXML_DOCUMENT )
  891. {
  892.     tabsize = 4;
  893.     useMicrosoftBOM = false;
  894.     ClearError();
  895. }
  896.  
  897. TiXmlDocument::TiXmlDocument( const char * documentName ) : TiXmlNode( TiXmlNode::TINYXML_DOCUMENT )
  898. {
  899.     tabsize = 4;
  900.     useMicrosoftBOM = false;
  901.     value = documentName;
  902.     ClearError();
  903. }
  904.  
  905.  
  906. #ifdef TIXML_USE_STL
  907. TiXmlDocument::TiXmlDocument( const std::string& documentName ) : TiXmlNode( TiXmlNode::TINYXML_DOCUMENT )
  908. {
  909.     tabsize = 4;
  910.     useMicrosoftBOM = false;
  911.     value = documentName;
  912.     ClearError();
  913. }
  914. #endif
  915.  
  916.  
  917. TiXmlDocument::TiXmlDocument( const TiXmlDocument& copy ) : TiXmlNode( TiXmlNode::TINYXML_DOCUMENT )
  918. {
  919.     copy.CopyTo( this );
  920. }
  921.  
  922.  
  923. TiXmlDocument& TiXmlDocument::operator=( const TiXmlDocument& copy )
  924. {
  925.     Clear();
  926.     copy.CopyTo( this );
  927.     return *this;
  928. }
  929.  
  930.  
  931. bool TiXmlDocument::LoadFile( TiXmlEncoding encoding )
  932. {
  933.     return LoadFile( Value(), encoding );
  934. }
  935.  
  936.  
  937. bool TiXmlDocument::SaveFile() const
  938. {
  939.     return SaveFile( Value() );
  940. }
  941.  
  942. bool TiXmlDocument::LoadFile( const char* _filename, TiXmlEncoding encoding )
  943. {
  944.     TIXML_STRING filename( _filename );
  945.     value = filename;
  946.  
  947.     // reading in binary mode so that tinyxml can normalize the EOL
  948.     FILE* file = TiXmlFOpen( value.c_str (), "rb" );    
  949.  
  950.     if ( file )
  951.     {
  952.         bool result = LoadFile( file, encoding );
  953.         fclose( file );
  954.         return result;
  955.     }
  956.     else
  957.     {
  958.         SetError( TIXML_ERROR_OPENING_FILE, 0, 0, TIXML_ENCODING_UNKNOWN );
  959.         return false;
  960.     }
  961. }
  962.  
  963. bool TiXmlDocument::LoadFile( FILE* file, TiXmlEncoding encoding )
  964. {
  965.     if ( !file ) 
  966.     {
  967.         SetError( TIXML_ERROR_OPENING_FILE, 0, 0, TIXML_ENCODING_UNKNOWN );
  968.         return false;
  969.     }
  970.  
  971.     // Delete the existing data:
  972.     Clear();
  973.     location.Clear();
  974.  
  975.     // Get the file size, so we can pre-allocate the string. HUGE speed impact.
  976.     long length = 0;
  977.     fseek( file, 0, SEEK_END );
  978.     length = ftell( file );
  979.     fseek( file, 0, SEEK_SET );
  980.  
  981.     // Strange case, but good to handle up front.
  982.     if ( length <= 0 )
  983.     {
  984.         SetError( TIXML_ERROR_DOCUMENT_EMPTY, 0, 0, TIXML_ENCODING_UNKNOWN );
  985.         return false;
  986.     }
  987.  
  988.     // Subtle bug here. TinyXml did use fgets. But from the XML spec:
  989.     // 2.11 End-of-Line Handling
  990.     // <snip>
  991.     // <quote>
  992.     // ...the XML processor MUST behave as if it normalized all line breaks in external 
  993.     // parsed entities (including the document entity) on input, before parsing, by translating 
  994.     // both the two-character sequence #xD #xA and any #xD that is not followed by #xA to 
  995.     // a single #xA character.
  996.     // </quote>
  997.     //
  998.     // It is not clear fgets does that, and certainly isn't clear it works cross platform. 
  999.     // Generally, you expect fgets to translate from the convention of the OS to the c/unix
  1000.     // convention, and not work generally.
  1001.  
  1002.     /*
  1003.     while( fgets( buf, sizeof(buf), file ) )
  1004.     {
  1005.         data += buf;
  1006.     }
  1007.     */
  1008.  
  1009.     char* buf = new char[ length+1 ];
  1010.     buf[0] = 0;
  1011.  
  1012.     if ( fread( buf, length, 1, file ) != 1 ) {
  1013.         delete [] buf;
  1014.         SetError( TIXML_ERROR_OPENING_FILE, 0, 0, TIXML_ENCODING_UNKNOWN );
  1015.         return false;
  1016.     }
  1017.  
  1018.     // Process the buffer in place to normalize new lines. (See comment above.)
  1019.     // Copies from the 'p' to 'q' pointer, where p can advance faster if
  1020.     // a newline-carriage return is hit.
  1021.     //
  1022.     // Wikipedia:
  1023.     // Systems based on ASCII or a compatible character set use either LF  (Line feed, '\n', 0x0A, 10 in decimal) or 
  1024.     // CR (Carriage return, '\r', 0x0D, 13 in decimal) individually, or CR followed by LF (CR+LF, 0x0D 0x0A)...
  1025.     //        * LF:    Multics, Unix and Unix-like systems (GNU/Linux, AIX, Xenix, Mac OS X, FreeBSD, etc.), BeOS, Amiga, RISC OS, and others
  1026.     //        * CR+LF: DEC RT-11 and most other early non-Unix, non-IBM OSes, CP/M, MP/M, DOS, OS/2, Microsoft Windows, Symbian OS
  1027.     //        * CR:    Commodore 8-bit machines, Apple II family, Mac OS up to version 9 and OS-9
  1028.  
  1029.     const char* p = buf;    // the read head
  1030.     char* q = buf;            // the write head
  1031.     const char CR = 0x0d;
  1032.     const char LF = 0x0a;
  1033.  
  1034.     buf[length] = 0;
  1035.     while( *p ) {
  1036.         assert( p < (buf+length) );
  1037.         assert( q <= (buf+length) );
  1038.         assert( q <= p );
  1039.  
  1040.         if ( *p == CR ) {
  1041.             *q++ = LF;
  1042.             p++;
  1043.             if ( *p == LF ) {        // check for CR+LF (and skip LF)
  1044.                 p++;
  1045.             }
  1046.         }
  1047.         else {
  1048.             *q++ = *p++;
  1049.         }
  1050.     }
  1051.     assert( q <= (buf+length) );
  1052.     *q = 0;
  1053.  
  1054.     Parse( buf, 0, encoding );
  1055.  
  1056.     delete [] buf;
  1057.     return !Error();
  1058. }
  1059.  
  1060.  
  1061. bool TiXmlDocument::SaveFile( const char * filename ) const
  1062. {
  1063.     // The old c stuff lives on...
  1064.     FILE* fp = TiXmlFOpen( filename, "w" );
  1065.     if ( fp )
  1066.     {
  1067.         bool result = SaveFile( fp );
  1068.         fclose( fp );
  1069.         return result;
  1070.     }
  1071.     return false;
  1072. }
  1073.  
  1074.  
  1075. bool TiXmlDocument::SaveFile( FILE* fp ) const
  1076. {
  1077.     if ( useMicrosoftBOM ) 
  1078.     {
  1079.         const unsigned char TIXML_UTF_LEAD_0 = 0xefU;
  1080.         const unsigned char TIXML_UTF_LEAD_1 = 0xbbU;
  1081.         const unsigned char TIXML_UTF_LEAD_2 = 0xbfU;
  1082.  
  1083.         fputc( TIXML_UTF_LEAD_0, fp );
  1084.         fputc( TIXML_UTF_LEAD_1, fp );
  1085.         fputc( TIXML_UTF_LEAD_2, fp );
  1086.     }
  1087.     Print( fp, 0 );
  1088.     return (ferror(fp) == 0);
  1089. }
  1090.  
  1091.  
  1092. void TiXmlDocument::CopyTo( TiXmlDocument* target ) const
  1093. {
  1094.     TiXmlNode::CopyTo( target );
  1095.  
  1096.     target->error = error;
  1097.     target->errorId = errorId;
  1098.     target->errorDesc = errorDesc;
  1099.     target->tabsize = tabsize;
  1100.     target->errorLocation = errorLocation;
  1101.     target->useMicrosoftBOM = useMicrosoftBOM;
  1102.  
  1103.     TiXmlNode* node = 0;
  1104.     for ( node = firstChild; node; node = node->NextSibling() )
  1105.     {
  1106.         target->LinkEndChild( node->Clone() );
  1107.     }    
  1108. }
  1109.  
  1110.  
  1111. TiXmlNode* TiXmlDocument::Clone() const
  1112. {
  1113.     TiXmlDocument* clone = new TiXmlDocument();
  1114.     if ( !clone )
  1115.         return 0;
  1116.  
  1117.     CopyTo( clone );
  1118.     return clone;
  1119. }
  1120.  
  1121.  
  1122. void TiXmlDocument::Print( FILE* cfile, int depth ) const
  1123. {
  1124.     assert( cfile );
  1125.     for ( const TiXmlNode* node=FirstChild(); node; node=node->NextSibling() )
  1126.     {
  1127.         node->Print( cfile, depth );
  1128.         fprintf( cfile, "\n" );
  1129.     }
  1130. }
  1131.  
  1132.  
  1133. bool TiXmlDocument::Accept( TiXmlVisitor* visitor ) const
  1134. {
  1135.     if ( visitor->VisitEnter( *this ) )
  1136.     {
  1137.         for ( const TiXmlNode* node=FirstChild(); node; node=node->NextSibling() )
  1138.         {
  1139.             if ( !node->Accept( visitor ) )
  1140.                 break;
  1141.         }
  1142.     }
  1143.     return visitor->VisitExit( *this );
  1144. }
  1145.  
  1146.  
  1147. const TiXmlAttribute* TiXmlAttribute::Next() const
  1148. {
  1149.     // We are using knowledge of the sentinel. The sentinel
  1150.     // have a value or name.
  1151.     if ( next->value.empty() && next->name.empty() )
  1152.         return 0;
  1153.     return next;
  1154. }
  1155.  
  1156. /*
  1157. TiXmlAttribute* TiXmlAttribute::Next()
  1158. {
  1159.     // We are using knowledge of the sentinel. The sentinel
  1160.     // have a value or name.
  1161.     if ( next->value.empty() && next->name.empty() )
  1162.         return 0;
  1163.     return next;
  1164. }
  1165. */
  1166.  
  1167. const TiXmlAttribute* TiXmlAttribute::Previous() const
  1168. {
  1169.     // We are using knowledge of the sentinel. The sentinel
  1170.     // have a value or name.
  1171.     if ( prev->value.empty() && prev->name.empty() )
  1172.         return 0;
  1173.     return prev;
  1174. }
  1175.  
  1176. /*
  1177. TiXmlAttribute* TiXmlAttribute::Previous()
  1178. {
  1179.     // We are using knowledge of the sentinel. The sentinel
  1180.     // have a value or name.
  1181.     if ( prev->value.empty() && prev->name.empty() )
  1182.         return 0;
  1183.     return prev;
  1184. }
  1185. */
  1186.  
  1187. void TiXmlAttribute::Print( FILE* cfile, int /*depth*/, TIXML_STRING* str ) const
  1188. {
  1189.     TIXML_STRING n, v;
  1190.  
  1191.     EncodeString( name, &n );
  1192.     EncodeString( value, &v );
  1193.  
  1194.     if (value.find ('\"') == TIXML_STRING::npos) {
  1195.         if ( cfile ) {
  1196.             fprintf (cfile, "%s=\"%s\"", n.c_str(), v.c_str() );
  1197.         }
  1198.         if ( str ) {
  1199.             (*str) += n; (*str) += "=\""; (*str) += v; (*str) += "\"";
  1200.         }
  1201.     }
  1202.     else {
  1203.         if ( cfile ) {
  1204.             fprintf (cfile, "%s='%s'", n.c_str(), v.c_str() );
  1205.         }
  1206.         if ( str ) {
  1207.             (*str) += n; (*str) += "='"; (*str) += v; (*str) += "'";
  1208.         }
  1209.     }
  1210. }
  1211.  
  1212.  
  1213. int TiXmlAttribute::QueryIntValue( int* ival ) const
  1214. {
  1215.     if ( TIXML_SSCANF( value.c_str(), "%d", ival ) == 1 )
  1216.         return TIXML_SUCCESS;
  1217.     return TIXML_WRONG_TYPE;
  1218. }
  1219.  
  1220. int TiXmlAttribute::QueryDoubleValue( double* dval ) const
  1221. {
  1222.     if ( TIXML_SSCANF( value.c_str(), "%lf", dval ) == 1 )
  1223.         return TIXML_SUCCESS;
  1224.     return TIXML_WRONG_TYPE;
  1225. }
  1226.  
  1227. void TiXmlAttribute::SetIntValue( int _value )
  1228. {
  1229.     char buf [64];
  1230.     #if defined(TIXML_SNPRINTF)        
  1231.         TIXML_SNPRINTF(buf, sizeof(buf), "%d", _value);
  1232.     #else
  1233.         sprintf (buf, "%d", _value);
  1234.     #endif
  1235.     SetValue (buf);
  1236. }
  1237.  
  1238. void TiXmlAttribute::SetDoubleValue( double _value )
  1239. {
  1240.     char buf [256];
  1241.     #if defined(TIXML_SNPRINTF)        
  1242.         TIXML_SNPRINTF( buf, sizeof(buf), "%g", _value);
  1243.     #else
  1244.         sprintf (buf, "%g", _value);
  1245.     #endif
  1246.     SetValue (buf);
  1247. }
  1248.  
  1249. int TiXmlAttribute::IntValue() const
  1250. {
  1251.     return atoi (value.c_str ());
  1252. }
  1253.  
  1254. double  TiXmlAttribute::DoubleValue() const
  1255. {
  1256.     return atof (value.c_str ());
  1257. }
  1258.  
  1259.  
  1260. TiXmlComment::TiXmlComment( const TiXmlComment& copy ) : TiXmlNode( TiXmlNode::TINYXML_COMMENT )
  1261. {
  1262.     copy.CopyTo( this );
  1263. }
  1264.  
  1265.  
  1266. TiXmlComment& TiXmlComment::operator=( const TiXmlComment& base )
  1267. {
  1268.     Clear();
  1269.     base.CopyTo( this );
  1270.     return *this;
  1271. }
  1272.  
  1273.  
  1274. void TiXmlComment::Print( FILE* cfile, int depth ) const
  1275. {
  1276.     assert( cfile );
  1277.     for ( int i=0; i<depth; i++ )
  1278.     {
  1279.         fprintf( cfile,  "    " );
  1280.     }
  1281.     fprintf( cfile, "<!--%s-->", value.c_str() );
  1282. }
  1283.  
  1284.  
  1285. void TiXmlComment::CopyTo( TiXmlComment* target ) const
  1286. {
  1287.     TiXmlNode::CopyTo( target );
  1288. }
  1289.  
  1290.  
  1291. bool TiXmlComment::Accept( TiXmlVisitor* visitor ) const
  1292. {
  1293.     return visitor->Visit( *this );
  1294. }
  1295.  
  1296.  
  1297. TiXmlNode* TiXmlComment::Clone() const
  1298. {
  1299.     TiXmlComment* clone = new TiXmlComment();
  1300.  
  1301.     if ( !clone )
  1302.         return 0;
  1303.  
  1304.     CopyTo( clone );
  1305.     return clone;
  1306. }
  1307.  
  1308.  
  1309. void TiXmlText::Print( FILE* cfile, int depth ) const
  1310. {
  1311.     assert( cfile );
  1312.     if ( cdata )
  1313.     {
  1314.         int i;
  1315.         fprintf( cfile, "\n" );
  1316.         for ( i=0; i<depth; i++ ) {
  1317.             fprintf( cfile, "    " );
  1318.         }
  1319.         fprintf( cfile, "<![CDATA[%s]]>\n", value.c_str() );    // unformatted output
  1320.     }
  1321.     else
  1322.     {
  1323.         TIXML_STRING buffer;
  1324.         EncodeString( value, &buffer );
  1325.         fprintf( cfile, "%s", buffer.c_str() );
  1326.     }
  1327. }
  1328.  
  1329.  
  1330. void TiXmlText::CopyTo( TiXmlText* target ) const
  1331. {
  1332.     TiXmlNode::CopyTo( target );
  1333.     target->cdata = cdata;
  1334. }
  1335.  
  1336.  
  1337. bool TiXmlText::Accept( TiXmlVisitor* visitor ) const
  1338. {
  1339.     return visitor->Visit( *this );
  1340. }
  1341.  
  1342.  
  1343. TiXmlNode* TiXmlText::Clone() const
  1344. {    
  1345.     TiXmlText* clone = 0;
  1346.     clone = new TiXmlText( "" );
  1347.  
  1348.     if ( !clone )
  1349.         return 0;
  1350.  
  1351.     CopyTo( clone );
  1352.     return clone;
  1353. }
  1354.  
  1355.  
  1356. TiXmlDeclaration::TiXmlDeclaration( const char * _version,
  1357.                                     const char * _encoding,
  1358.                                     const char * _standalone )
  1359.     : TiXmlNode( TiXmlNode::TINYXML_DECLARATION )
  1360. {
  1361.     version = _version;
  1362.     encoding = _encoding;
  1363.     standalone = _standalone;
  1364. }
  1365.  
  1366.  
  1367. #ifdef TIXML_USE_STL
  1368. TiXmlDeclaration::TiXmlDeclaration(    const std::string& _version,
  1369.                                     const std::string& _encoding,
  1370.                                     const std::string& _standalone )
  1371.     : TiXmlNode( TiXmlNode::TINYXML_DECLARATION )
  1372. {
  1373.     version = _version;
  1374.     encoding = _encoding;
  1375.     standalone = _standalone;
  1376. }
  1377. #endif
  1378.  
  1379.  
  1380. TiXmlDeclaration::TiXmlDeclaration( const TiXmlDeclaration& copy )
  1381.     : TiXmlNode( TiXmlNode::TINYXML_DECLARATION )
  1382. {
  1383.     copy.CopyTo( this );    
  1384. }
  1385.  
  1386.  
  1387. TiXmlDeclaration& TiXmlDeclaration::operator=( const TiXmlDeclaration& copy )
  1388. {
  1389.     Clear();
  1390.     copy.CopyTo( this );
  1391.     return *this;
  1392. }
  1393.  
  1394.  
  1395. void TiXmlDeclaration::Print( FILE* cfile, int /*depth*/, TIXML_STRING* str ) const
  1396. {
  1397.     if ( cfile ) fprintf( cfile, "<?xml " );
  1398.     if ( str )     (*str) += "<?xml ";
  1399.  
  1400.     if ( !version.empty() ) {
  1401.         if ( cfile ) fprintf (cfile, "version=\"%s\" ", version.c_str ());
  1402.         if ( str ) { (*str) += "version=\""; (*str) += version; (*str) += "\" "; }
  1403.     }
  1404.     if ( !encoding.empty() ) {
  1405.         if ( cfile ) fprintf (cfile, "encoding=\"%s\" ", encoding.c_str ());
  1406.         if ( str ) { (*str) += "encoding=\""; (*str) += encoding; (*str) += "\" "; }
  1407.     }
  1408.     if ( !standalone.empty() ) {
  1409.         if ( cfile ) fprintf (cfile, "standalone=\"%s\" ", standalone.c_str ());
  1410.         if ( str ) { (*str) += "standalone=\""; (*str) += standalone; (*str) += "\" "; }
  1411.     }
  1412.     if ( cfile ) fprintf( cfile, "?>" );
  1413.     if ( str )     (*str) += "?>";
  1414. }
  1415.  
  1416.  
  1417. void TiXmlDeclaration::CopyTo( TiXmlDeclaration* target ) const
  1418. {
  1419.     TiXmlNode::CopyTo( target );
  1420.  
  1421.     target->version = version;
  1422.     target->encoding = encoding;
  1423.     target->standalone = standalone;
  1424. }
  1425.  
  1426.  
  1427. bool TiXmlDeclaration::Accept( TiXmlVisitor* visitor ) const
  1428. {
  1429.     return visitor->Visit( *this );
  1430. }
  1431.  
  1432.  
  1433. TiXmlNode* TiXmlDeclaration::Clone() const
  1434. {    
  1435.     TiXmlDeclaration* clone = new TiXmlDeclaration();
  1436.  
  1437.     if ( !clone )
  1438.         return 0;
  1439.  
  1440.     CopyTo( clone );
  1441.     return clone;
  1442. }
  1443.  
  1444.  
  1445. void TiXmlUnknown::Print( FILE* cfile, int depth ) const
  1446. {
  1447.     for ( int i=0; i<depth; i++ )
  1448.         fprintf( cfile, "    " );
  1449.     fprintf( cfile, "<%s>", value.c_str() );
  1450. }
  1451.  
  1452.  
  1453. void TiXmlUnknown::CopyTo( TiXmlUnknown* target ) const
  1454. {
  1455.     TiXmlNode::CopyTo( target );
  1456. }
  1457.  
  1458.  
  1459. bool TiXmlUnknown::Accept( TiXmlVisitor* visitor ) const
  1460. {
  1461.     return visitor->Visit( *this );
  1462. }
  1463.  
  1464.  
  1465. TiXmlNode* TiXmlUnknown::Clone() const
  1466. {
  1467.     TiXmlUnknown* clone = new TiXmlUnknown();
  1468.  
  1469.     if ( !clone )
  1470.         return 0;
  1471.  
  1472.     CopyTo( clone );
  1473.     return clone;
  1474. }
  1475.  
  1476.  
  1477. TiXmlAttributeSet::TiXmlAttributeSet()
  1478. {
  1479.     sentinel.next = &sentinel;
  1480.     sentinel.prev = &sentinel;
  1481. }
  1482.  
  1483.  
  1484. TiXmlAttributeSet::~TiXmlAttributeSet()
  1485. {
  1486.     assert( sentinel.next == &sentinel );
  1487.     assert( sentinel.prev == &sentinel );
  1488. }
  1489.  
  1490.  
  1491. void TiXmlAttributeSet::Add( TiXmlAttribute* addMe )
  1492. {
  1493.     #ifdef TIXML_USE_STL
  1494.     assert( !Find( TIXML_STRING( addMe->Name() ) ) );    // Shouldn't be multiply adding to the set.
  1495.     #else
  1496.     assert( !Find( addMe->Name() ) );    // Shouldn't be multiply adding to the set.
  1497.     #endif
  1498.  
  1499.     addMe->next = &sentinel;
  1500.     addMe->prev = sentinel.prev;
  1501.  
  1502.     sentinel.prev->next = addMe;
  1503.     sentinel.prev      = addMe;
  1504. }
  1505.  
  1506. void TiXmlAttributeSet::Remove( TiXmlAttribute* removeMe )
  1507. {
  1508.     TiXmlAttribute* node;
  1509.  
  1510.     for( node = sentinel.next; node != &sentinel; node = node->next )
  1511.     {
  1512.         if ( node == removeMe )
  1513.         {
  1514.             node->prev->next = node->next;
  1515.             node->next->prev = node->prev;
  1516.             node->next = 0;
  1517.             node->prev = 0;
  1518.             return;
  1519.         }
  1520.     }
  1521.     assert( 0 );        // we tried to remove a non-linked attribute.
  1522. }
  1523.  
  1524.  
  1525. #ifdef TIXML_USE_STL
  1526. TiXmlAttribute* TiXmlAttributeSet::Find( const std::string& name ) const
  1527. {
  1528.     for( TiXmlAttribute* node = sentinel.next; node != &sentinel; node = node->next )
  1529.     {
  1530.         if ( node->name == name )
  1531.             return node;
  1532.     }
  1533.     return 0;
  1534. }
  1535.  
  1536. TiXmlAttribute* TiXmlAttributeSet::FindOrCreate( const std::string& _name )
  1537. {
  1538.     TiXmlAttribute* attrib = Find( _name );
  1539.     if ( !attrib ) {
  1540.         attrib = new TiXmlAttribute();
  1541.         Add( attrib );
  1542.         attrib->SetName( _name );
  1543.     }
  1544.     return attrib;
  1545. }
  1546. #endif
  1547.  
  1548.  
  1549. TiXmlAttribute* TiXmlAttributeSet::Find( const char* name ) const
  1550. {
  1551.     for( TiXmlAttribute* node = sentinel.next; node != &sentinel; node = node->next )
  1552.     {
  1553.         if ( strcmp( node->name.c_str(), name ) == 0 )
  1554.             return node;
  1555.     }
  1556.     return 0;
  1557. }
  1558.  
  1559.  
  1560. TiXmlAttribute* TiXmlAttributeSet::FindOrCreate( const char* _name )
  1561. {
  1562.     TiXmlAttribute* attrib = Find( _name );
  1563.     if ( !attrib ) {
  1564.         attrib = new TiXmlAttribute();
  1565.         Add( attrib );
  1566.         attrib->SetName( _name );
  1567.     }
  1568.     return attrib;
  1569. }
  1570.  
  1571.  
  1572. #ifdef TIXML_USE_STL    
  1573. std::istream& operator>> (std::istream & in, TiXmlNode & base)
  1574. {
  1575.     TIXML_STRING tag;
  1576.     tag.reserve( 8 * 1000 );
  1577.     base.StreamIn( &in, &tag );
  1578.  
  1579.     base.Parse( tag.c_str(), 0, TIXML_DEFAULT_ENCODING );
  1580.     return in;
  1581. }
  1582. #endif
  1583.  
  1584.  
  1585. #ifdef TIXML_USE_STL    
  1586. std::ostream& operator<< (std::ostream & out, const TiXmlNode & base)
  1587. {
  1588.     TiXmlPrinter printer;
  1589.     printer.SetStreamPrinting();
  1590.     base.Accept( &printer );
  1591.     out << printer.Str();
  1592.  
  1593.     return out;
  1594. }
  1595.  
  1596.  
  1597. std::string& operator<< (std::string& out, const TiXmlNode& base )
  1598. {
  1599.     TiXmlPrinter printer;
  1600.     printer.SetStreamPrinting();
  1601.     base.Accept( &printer );
  1602.     out.append( printer.Str() );
  1603.  
  1604.     return out;
  1605. }
  1606. #endif
  1607.  
  1608.  
  1609. TiXmlHandle TiXmlHandle::FirstChild() const
  1610. {
  1611.     if ( node )
  1612.     {
  1613.         TiXmlNode* child = node->FirstChild();
  1614.         if ( child )
  1615.             return TiXmlHandle( child );
  1616.     }
  1617.     return TiXmlHandle( 0 );
  1618. }
  1619.  
  1620.  
  1621. TiXmlHandle TiXmlHandle::FirstChild( const char * value ) const
  1622. {
  1623.     if ( node )
  1624.     {
  1625.         TiXmlNode* child = node->FirstChild( value );
  1626.         if ( child )
  1627.             return TiXmlHandle( child );
  1628.     }
  1629.     return TiXmlHandle( 0 );
  1630. }
  1631.  
  1632.  
  1633. TiXmlHandle TiXmlHandle::FirstChildElement() const
  1634. {
  1635.     if ( node )
  1636.     {
  1637.         TiXmlElement* child = node->FirstChildElement();
  1638.         if ( child )
  1639.             return TiXmlHandle( child );
  1640.     }
  1641.     return TiXmlHandle( 0 );
  1642. }
  1643.  
  1644.  
  1645. TiXmlHandle TiXmlHandle::FirstChildElement( const char * value ) const
  1646. {
  1647.     if ( node )
  1648.     {
  1649.         TiXmlElement* child = node->FirstChildElement( value );
  1650.         if ( child )
  1651.             return TiXmlHandle( child );
  1652.     }
  1653.     return TiXmlHandle( 0 );
  1654. }
  1655.  
  1656.  
  1657. TiXmlHandle TiXmlHandle::Child( int count ) const
  1658. {
  1659.     if ( node )
  1660.     {
  1661.         int i;
  1662.         TiXmlNode* child = node->FirstChild();
  1663.         for (    i=0;
  1664.                 child && i<count;
  1665.                 child = child->NextSibling(), ++i )
  1666.         {
  1667.             // nothing
  1668.         }
  1669.         if ( child )
  1670.             return TiXmlHandle( child );
  1671.     }
  1672.     return TiXmlHandle( 0 );
  1673. }
  1674.  
  1675.  
  1676. TiXmlHandle TiXmlHandle::Child( const char* value, int count ) const
  1677. {
  1678.     if ( node )
  1679.     {
  1680.         int i;
  1681.         TiXmlNode* child = node->FirstChild( value );
  1682.         for (    i=0;
  1683.                 child && i<count;
  1684.                 child = child->NextSibling( value ), ++i )
  1685.         {
  1686.             // nothing
  1687.         }
  1688.         if ( child )
  1689.             return TiXmlHandle( child );
  1690.     }
  1691.     return TiXmlHandle( 0 );
  1692. }
  1693.  
  1694.  
  1695. TiXmlHandle TiXmlHandle::ChildElement( int count ) const
  1696. {
  1697.     if ( node )
  1698.     {
  1699.         int i;
  1700.         TiXmlElement* child = node->FirstChildElement();
  1701.         for (    i=0;
  1702.                 child && i<count;
  1703.                 child = child->NextSiblingElement(), ++i )
  1704.         {
  1705.             // nothing
  1706.         }
  1707.         if ( child )
  1708.             return TiXmlHandle( child );
  1709.     }
  1710.     return TiXmlHandle( 0 );
  1711. }
  1712.  
  1713.  
  1714. TiXmlHandle TiXmlHandle::ChildElement( const char* value, int count ) const
  1715. {
  1716.     if ( node )
  1717.     {
  1718.         int i;
  1719.         TiXmlElement* child = node->FirstChildElement( value );
  1720.         for (    i=0;
  1721.                 child && i<count;
  1722.                 child = child->NextSiblingElement( value ), ++i )
  1723.         {
  1724.             // nothing
  1725.         }
  1726.         if ( child )
  1727.             return TiXmlHandle( child );
  1728.     }
  1729.     return TiXmlHandle( 0 );
  1730. }
  1731.  
  1732.  
  1733. bool TiXmlPrinter::VisitEnter( const TiXmlDocument& )
  1734. {
  1735.     return true;
  1736. }
  1737.  
  1738. bool TiXmlPrinter::VisitExit( const TiXmlDocument& )
  1739. {
  1740.     return true;
  1741. }
  1742.  
  1743. bool TiXmlPrinter::VisitEnter( const TiXmlElement& element, const TiXmlAttribute* firstAttribute )
  1744. {
  1745.     DoIndent();
  1746.     buffer += "<";
  1747.     buffer += element.Value();
  1748.  
  1749.     for( const TiXmlAttribute* attrib = firstAttribute; attrib; attrib = attrib->Next() )
  1750.     {
  1751.         buffer += " ";
  1752.         attrib->Print( 0, 0, &buffer );
  1753.     }
  1754.  
  1755.     if ( !element.FirstChild() ) 
  1756.     {
  1757.         buffer += " />";
  1758.         DoLineBreak();
  1759.     }
  1760.     else 
  1761.     {
  1762.         buffer += ">";
  1763.         if (    element.FirstChild()->ToText()
  1764.               && element.LastChild() == element.FirstChild()
  1765.               && element.FirstChild()->ToText()->CDATA() == false )
  1766.         {
  1767.             simpleTextPrint = true;
  1768.             // no DoLineBreak()!
  1769.         }
  1770.         else
  1771.         {
  1772.             DoLineBreak();
  1773.         }
  1774.     }
  1775.     ++depth;    
  1776.     return true;
  1777. }
  1778.  
  1779.  
  1780. bool TiXmlPrinter::VisitExit( const TiXmlElement& element )
  1781. {
  1782.     --depth;
  1783.     if ( !element.FirstChild() ) 
  1784.     {
  1785.         // nothing.
  1786.     }
  1787.     else 
  1788.     {
  1789.         if ( simpleTextPrint )
  1790.         {
  1791.             simpleTextPrint = false;
  1792.         }
  1793.         else
  1794.         {
  1795.             DoIndent();
  1796.         }
  1797.         buffer += "</";
  1798.         buffer += element.Value();
  1799.         buffer += ">";
  1800.         DoLineBreak();
  1801.     }
  1802.     return true;
  1803. }
  1804.  
  1805.  
  1806. bool TiXmlPrinter::Visit( const TiXmlText& text )
  1807. {
  1808.     if ( text.CDATA() )
  1809.     {
  1810.         DoIndent();
  1811.         buffer += "<![CDATA[";
  1812.         buffer += text.Value();
  1813.         buffer += "]]>";
  1814.         DoLineBreak();
  1815.     }
  1816.     else if ( simpleTextPrint )
  1817.     {
  1818.         TIXML_STRING str;
  1819.         TiXmlBase::EncodeString( text.ValueTStr(), &str );
  1820.         buffer += str;
  1821.     }
  1822.     else
  1823.     {
  1824.         DoIndent();
  1825.         TIXML_STRING str;
  1826.         TiXmlBase::EncodeString( text.ValueTStr(), &str );
  1827.         buffer += str;
  1828.         DoLineBreak();
  1829.     }
  1830.     return true;
  1831. }
  1832.  
  1833.  
  1834. bool TiXmlPrinter::Visit( const TiXmlDeclaration& declaration )
  1835. {
  1836.     DoIndent();
  1837.     declaration.Print( 0, 0, &buffer );
  1838.     DoLineBreak();
  1839.     return true;
  1840. }
  1841.  
  1842.  
  1843. bool TiXmlPrinter::Visit( const TiXmlComment& comment )
  1844. {
  1845.     DoIndent();
  1846.     buffer += "<!--";
  1847.     buffer += comment.Value();
  1848.     buffer += "-->";
  1849.     DoLineBreak();
  1850.     return true;
  1851. }
  1852.  
  1853.  
  1854. bool TiXmlPrinter::Visit( const TiXmlUnknown& unknown )
  1855. {
  1856.     DoIndent();
  1857.     buffer += "<";
  1858.     buffer += unknown.Value();
  1859.     buffer += ">";
  1860.     DoLineBreak();
  1861.     return true;
  1862. }
  1863.  
  1864.